package com.gm.app.web;

import com.gm.app.data.PlaceDAO;
import com.gm.app.model.Address;
import com.gm.app.model.City;
import com.gm.app.model.Contact;
import com.gm.app.model.Country;
import com.gm.app.model.District;
import com.gm.app.model.Region;
import com.gm.app.model.Register;
import com.gm.app.model.Street;

public abstract class RegisterController {

	protected <T extends Register> T update(T i) {

		if (i.getId() == null) {

			Address p = null;
			Country addrCountry = null;

			if (i.getAddress().getDescription() != null) {
				p = PlaceDAO.find(Address.findAddressesByDescription(i
						.getAddress().getDescription()));
			}
			if (p == null) {
				City addrCity;
				Region addrState;
				Street addrStreet;
				District addrDist;

				addrStreet = i.getAddress().getStreet();
				if (addrStreet != null && addrStreet.getDescription() != null
						&& addrStreet.getDescription().length() > 0) {
					addrStreet = PlaceDAO.find(Street
							.findStreetsByDescription(addrStreet
									.getDescription()));
					if (addrStreet != null) {
						i.getAddress().setStreet(addrStreet);
					} else {
						// i.getAddress().getState().persist();
					}
				}

				addrDist = i.getAddress().getDistrict();
				if (addrDist != null && addrDist.getDescription() != null
						&& addrDist.getDescription().length() > 0) {
					addrDist = PlaceDAO.find(District
							.findDistrictsByDescription(addrDist
									.getDescription()));
					if (addrDist != null) {
						i.getAddress().setDistrict(addrDist);
					} else {
						// i.getAddress().getState().persist();
					}
				}

				addrCity = i.getAddress().getCity();
				if (addrCity != null && addrCity.getDescription() != null
						&& addrCity.getDescription().length() > 0) {
					addrCity = PlaceDAO.find(City
							.findCitysByDescription(addrCity.getDescription()));
					if (addrCity != null) {
						i.getAddress().setCity(addrCity);
					} else {
						// i.getAddress().getCity().persist();
					}
				}

				addrState = i.getAddress().getState();
				if (addrState != null && addrState.getDescription() != null
						&& addrState.getDescription().length() > 0) {
					addrState = PlaceDAO.find(Region
							.findRegionsByDescription(addrState
									.getDescription()));
					if (addrState != null) {
						i.getAddress().setState(addrState);
					} else {
						// i.getAddress().getState().persist();
					}
				}

				addrCountry = i.getAddress().getCountry();
				if (addrCountry != null && addrCountry.getDescription() != null
						&& addrCountry.getDescription().length() > 0) {
					addrCountry = PlaceDAO.find(Country
							.findCountrysByDescription(addrCountry
									.getDescription()));
					if (addrCountry != null) {
						i.getAddress().setCountry(addrCountry);
					} else {
						i.getAddress().getCountry().persist();
					}
				}
				i.getAddress().setDescription(
						PlaceDAO.format(i.getAddress(), false));
			} else {
				i.setAddress(p);
			}
			Contact ct = i.getContact();
			if (ct != null) {
				Country c = ct.getCountry();
				if (c != null && c.getDescription() != null
						&& c.getDescription().length() > 0) {
					c = PlaceDAO.find(Country.findCountrysByDescription(ct
							.getCountry().getDescription()));
					if (c != null) {
						ct.setCountry(c);
					} else if (addrCountry != null
							&& ct.getCountry().getDescription()
									.equals(addrCountry.getDescription())) {
						ct.setCountry(addrCountry);
					} else {
						ct.getCountry().persist();
					}
				}
			}
			i.persist();
		}else{
			i.merge();
		}

		return i;
	}

}
